package com.scau.classOnline.sftp;

import com.jcraft.jsch.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Properties;

/**
 * Create by ji on 2019-07-11
 * 文件传输到远程服务器
 */
@Component
@ConfigurationProperties(ignoreInvalidFields = true,prefix = "sftp")
public class FTPClient {
    private Integer port;
    private String host;
    private String username;
    private String password;
    private String remotePicPath;
    private String remoteCourseWarePath;
    private Integer timeout;

    private ChannelSftp sftp;

    private Session session;

    private void connect() throws JSchException {
        JSch jSch = new JSch();
        session=jSch.getSession(username,host,port);
        session.setPassword(password);
        Properties sessionConfig = new Properties();
        // StrictHostKeyChecking
        // "如果设为"yes"，ssh将不会自动把计算机的密匙加入"$HOME/.ssh/known_hosts"文件，
        // 且一旦计算机的密匙发生了变化，就拒绝连接。
        sessionConfig.setProperty("StrictHostKeyChecking", "no");
        // 设置会话参数
        session.setConfig(sessionConfig);
        session.setTimeout(timeout);
        session.connect();
        Channel channel=session.openChannel("sftp");
        sftp= (ChannelSftp) channel;
        sftp.connect();
    }

    /**
     *
     * @param file
     * @param filepath 存放该文件的服务器绝对路径，即remotePicPath或remoteCourseWarePath
     * @param filename 文件名
     * @return
     */
    @Async("异步")
    public boolean upload(MultipartFile file, String filepath, String filename) {
        try {
            connect();
            sftp.cd(filepath);
            sftp.put(file.getInputStream(),filename);
        } catch (JSchException | SftpException | IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            close();
        }
        return true;
    }

    public void close(){
        sftp.disconnect();
        session.disconnect();
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRemotePicPath() {
        return remotePicPath;
    }

    public void setRemotePicPath(String remotePicPath) {
        this.remotePicPath = remotePicPath;
    }

    public String getRemoteCourseWarePath() {
        return remoteCourseWarePath;
    }

    public void setRemoteCourseWarePath(String remoteCourseWarePath) {
        this.remoteCourseWarePath = remoteCourseWarePath;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }
}
